

//引入md5加密
// var common = require('/md5.js');
// import md5 from '/md5.js';

let appid = '1110275544'; 
//let secret = 'THVy2coOYjdwvEmf';
var client_info = "{\"uniqid\":\"104C2BAC-6E2C-4723-A55E-F66FE1206D29\",\"os\":\"iOS11.2\",\"mac\":\"6C:40:08:8E:96:CE\",\"screen\":\"2436X1125\",\"from\":\"5000\",\"model\":\"Simulator\",\"other\":\",\",\"version\":\"9.1.6\"}"; 
let opApiKey = "1110275544";
let opSecretKey = "THVy2coOYjdwvEmf";

//请求地址
let apiurl = 'https://apim.renren.com/api/';

let versionStr = '2.3.14';
var wxpersonInfo;
var currentUser;



function init(localpage, wxInfo, userInfo) {
  _page = localpage;
  wxpersonInfo = wxInfo;
  currentUser = userInfo;
}
/**
*json转字符串
*/
function stringToJson(data) {
  return JSON.parse(data);
}
/**
*字符串转json
*/
function jsonToString(data) {
  return JSON.stringify(data);
}
/**
*map转换为json
*/
function mapToJson(map) {
  return JSON.stringify(strMapToObj(map));
}
/**
*json转换为map
*/
function jsonToMap(jsonStr) {
  return objToStrMap(JSON.parse(jsonStr));
}
/**
*map转化为对象（map所有键都是字符串，可以将其转换为对象）
*/
function strMapToObj(strMap) {
  let obj = Object.create(null);
  for (let [k, v] of strMap) {
    obj[k] = v;
  }
  return obj;
}
/**
*对象转换为Map
*/
function objToStrMap(obj) {
  let strMap = new Map();
  for (let k of Object.keys(obj)) {
    strMap.set(k, obj[k]);
  }
  return strMap;
}
/**
* 判断对象是否是字符串
*/
function isString(obj) {
  return Object.prototype.toString.call(obj) === "[object String]";
}

function getsign(jsonObj) {
  var jsonstr = this.objKeySort(jsonObj);
  // console.log(jsonstr);
  var secretKey = opSecretKey;
  if (typeof (currentUser.secret_key) != "undefined" && currentUser.secret_key && currentUser.secret_key.length > 0) {
    secretKey = currentUser.secret_key;
  }
  var newstr = jsonstr + secretKey;
  var sign = md5(newstr);
  return sign;
}

function getLoginsign(jsonObj) {
  var jsonstr = this.objKeySort(jsonObj);
  // console.log(jsonstr);
  var secretKey = opSecretKey;
  // console.log("==secretKey==" + secretKey);
  var newstr = jsonstr + secretKey;
  // console.log("newstr====" + newstr);
  var sign = md5(newstr);
  // console.log("===sign===" + sign);
  return sign;
}

function objKeySort(arys) {
  //先用Object内置类的keys方法获取要排序对象的属性名，再利用Array原型上的sort方法对获取的属性名进行排序，newkey是一个数组
  var jsonstr = "";
  var newkey = Object.keys(arys).sort();
  // console.log("newkey==" + newkey);
  for (var i = 0; i < newkey.length; i++) {
    //遍历newkey数组
    // console.log("===newkeyvalue===" + arys[newkey[i]]);
    var valuestr = arys[newkey[i]];
    if (typeof valuestr != "undefined" && valuestr && valuestr.length > 50) {
      valuestr = valuestr.substring(0, 50);
    }
    jsonstr = jsonstr + newkey[i] + "=" + valuestr;
  }
  return jsonstr; //返回排好序的新对象
}
/**
* 赋值对象
*/
function setuserinfo(userdata) {
  var uid = this.isString(userdata.uid) ? parseFloat(userdata.uid) : userdata.uid;
  var userinfo = {
    fill_stage: userdata.fill_stage,
    head_url: userdata.head_url,
    is_guide: userdata.is_guide,
    secret_key: userdata.secret_key,
    session_key: userdata.session_key,
    ticket: userdata.ticket,
    uid: uid,
    uniq_key: userdata.uniq_key,
    user_name: userdata.user_name,
    vip_icon_url: userdata.vip_icon_url,
    vip_url: userdata.vip_url,
    web_ticket: userdata.web_ticket,
  };
  var app = getApp();
  currentUser = userinfo;
  app.currentUser = userinfo;
}

/**
* save h5 cookies
*/
function saveLocalCookie(cookiesdata) {
  //保存_rtk
  try {
    //保存_rtk
    wx.setStorageSync("_rtk", cookiesdata._rtk);
    //保存格式化后的cookie
    wx.setStorageSync("cookiesdata", JSON.stringify(cookiesdata).replace(/,/g, ';').replace(/"/g, '').replace(/:/g, '=').replace(/{/g, '').replace(/}/g, '').replace(/ /g, ''));
  } catch (e) {
    console.log("saveLocalCookie storageSync exception" + e);
  }
}
function removeLocalCookie() {
  wx.removeStorageSync("cookiesdata");
}

function isBindMobile(){
  //是否绑定手机
  let url = apiurl + 'user/isBindMobileMulti';
  let data = {
    api_key: opApiKey,
    v: '1.0',
    call_id: new Date().getTime(),
    session_key: currentUser.session_key,  
  };
  var that = this;
  data.sig = that.getsign(data);
  wx.request({
    data: data,
    url: url,
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded; charset=utf-8'
    },
    success: function(res){
    	if(that.isBindMobileCallBack){
    		that.isBindMobileCallBack(res);
    	}
    }
  })
}
function newBindMobile(obj){
  let url = apiurl + 'user/newBindMobile';
  let data = {
    api_key: opApiKey,
    v: '1.0',
    call_id: new Date().getTime(),
    session_key: currentUser.session_key, 
    phone_number: obj.phone_number,
    captcha: obj.captcha,
    is_notify_user: 1,
    client_info: client_info
  };
  var that = this;
  data.sig = that.getsign(data);
  wx.request({
    data: data,
    url: url,
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded; charset=utf-8'
    },
    success: function(res){
    	if(that.newBindMobileCallBack){
    		that.newBindMobileCallBack(res);
    	}
    }
  })	
}
function newVerifyMobileBind(obj){
  let url = apiurl + 'user/newVerifyMobileBind';
  let data = {
    api_key: opApiKey,
    v: '1.0',
    call_id: new Date().getTime(),
    session_key: currentUser.session_key, 
    phone_number: obj.phone_number,
    //password: obj.password,
    // is_change_bind: 1,//这是换绑手机号的参数   这里不传
    client_info: client_info
  };
  var that = this;
  data.sig = that.getsign(data);
  wx.request({
    data: data,
    url: url,
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded; charset=utf-8'
    },
    success: function(res){
    	if(that.newVerifyMobileBindCallBack){
    		that.newVerifyMobileBindCallBack(res);
    	}
    }
  })	
}
module.exports = {
  // init: init,
  // stringToJson: stringToJson,
  // jsonToString: jsonToString,
  // mapToJson: mapToJson,
  // jsonToMap: jsonToMap,
  // strMapToObj: strMapToObj,
  // objToStrMap: objToStrMap,
  // isString: isString,
  // getsign: getsign,
  // getLoginsign: getLoginsign,
  // objKeySort: objKeySort,
  // setuserinfo: setuserinfo,
  // saveLocalCookie: saveLocalCookie,
  // removeLocalCookie: removeLocalCookie,
  // // loginrequest: loginrequest,
  // weixinCreateUser: weixinCreateUser,
  // createUserWithUserInfo: createUserWithUserInfo,
  // updateUserInfo: updateUserInfo,
  // updateLoginStatus: updateLoginStatus,
  isBindMobile: isBindMobile,
  newBindMobile: newBindMobile,
  newVerifyMobileBind: newVerifyMobileBind,
};

